package com.yjkf.zh.admin.service.impl;

import com.yjkf.zh.admin.dao.SysChannelMapper;
import com.yjkf.zh.admin.dao.SysPointUnitMapper;
import com.yjkf.zh.admin.dao.SysUserDeviceMapper;
import com.yjkf.zh.admin.model.SysChannel;
import com.yjkf.zh.admin.model.SysDevicePointUnit;
import com.yjkf.zh.admin.model.SysUserDevice;
import com.yjkf.zh.admin.service.SysPointUnitService;
import com.yjkf.zh.admin.service.SysUserDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysPointUnitServiceImpl implements SysPointUnitService {

    @Autowired
    private SysPointUnitMapper sysPointUnitMapper;

    @Autowired
    private SysUserDeviceMapper sysUserDeviceMapper;

    @Autowired
    private SysChannelMapper sysChannelMapper;


    @Override
    public List<SysDevicePointUnit> findAllById(Long id, HttpServletRequest request) {
        //根据设备的Id获取设备通道列表
        List<SysDevicePointUnit> sysDevicePointUnits = sysPointUnitMapper.findAllById(id);
      //  SysUserDevice sysUserDevice = sysUserDeviceMapper.findById(id);     //根据id获取UserDevice信息
        SysUserDevice sysUserDevice = sysUserDeviceMapper.findPointNum(id);
        Long userId = (Long)request.getSession().getAttribute("userId");
        SysUserDevice sudd=new SysUserDevice();
        sudd.setParentDeviceId(sysUserDevice.getParentDeviceId());
        sudd.setCreateTime(sysUserDevice.getCreateTime());
        sudd.setUserId(userId);
        List<SysUserDevice> sysUserDeviceu= sysUserDeviceMapper.findListForDate(sudd);
        Long sum= sysUserDeviceu .stream().collect(Collectors.summingLong(SysUserDevice::getPointNums));        //获取创建时间早于要查询的设备的通道和
        if(sysDevicePointUnits.isEmpty()){
            //获取根设备的通道数
            for(Long i = 0L;i<sysUserDevice.getPointNums();i++){
            //根据id查询通道名称
                long ide = i+1;
                Long dsx=i+1+sum;
                SysChannel sysChannel = sysChannelMapper.findChannelById(dsx);
                SysDevicePointUnit sysDevicePointUnit = new SysDevicePointUnit();
                    sysDevicePointUnit.setUserDeviceId(id);
                    sysDevicePointUnit.setIdentName(sysChannel.getIdentName());
                    sysDevicePointUnit.setPointName("通道"+dsx);
                    sysDevicePointUnit.setDataChannelId(dsx);
                    sysDevicePointUnit.setUnitName("℃");
                    sysDevicePointUnit.setPointIdent(sysChannel.getPointIdent());
                  // sysDevicePointUnit.setDeviceName(sysUserDevice.getDeviceName());
                    sysDevicePointUnits.add(sysDevicePointUnit);
            }
            return sysDevicePointUnits;
        }else{
            //查询当前通道数量

            List<SysDevicePointUnit> sysDevicePointUnits1 = new ArrayList<>();
                for(Long j =0L;j<sysUserDevice.getPointNums();j++){
                    long ide = j+1;
                    Long dsx=j+1+sum;

                    SysChannel sysChannel = sysChannelMapper.findChannelById(dsx);
                    SysDevicePointUnit sysDevicePointUnit = new SysDevicePointUnit();
                    sysDevicePointUnit.setUserDeviceId(id);
                    sysDevicePointUnit.setIdentName(sysChannel.getIdentName());
                    sysDevicePointUnit.setPointName("通道"+dsx);
                    sysDevicePointUnit.setUnitName("℃");
                    sysDevicePointUnit.setDataChannelId(dsx);
                    sysDevicePointUnit.setPointIdent(sysChannel.getPointIdent());
                  //sysDevicePointUnit.setDeviceName(sysUserDevice.getDeviceName());
                    sysDevicePointUnits1.add(sysDevicePointUnit);
                }
            Map<Long,SysDevicePointUnit> map = new HashMap<>();
                for(int k =0;k<sysDevicePointUnits1.size();k++){
                    SysDevicePointUnit sysDevicePointUnit = sysDevicePointUnits1.get(k);
                    map.put(sysDevicePointUnit.getDataChannelId(),sysDevicePointUnit);
                    for(SysDevicePointUnit sysDevicePointUnit2:sysDevicePointUnits){

                        if(sysDevicePointUnit2.getDataChannelId().equals(sysDevicePointUnit.getDataChannelId())){
                            sysDevicePointUnit2.setPointIdent(sysDevicePointUnit.getPointIdent());
                            map.put(sysDevicePointUnit2.getDataChannelId(),sysDevicePointUnit2);
                        }
                    }
                }
                List<SysDevicePointUnit> values = map.values().stream().collect(Collectors.toList());
                return values;
        }
    }

    @Override
    public int updatePointUnit(SysDevicePointUnit sysDevicePointUnit) {
        //判断传入的数据在PointUnit中是不是空的
        if(sysPointUnitMapper.findByTwoId(sysDevicePointUnit.getUserDeviceId(),sysDevicePointUnit.getDataChannelId())==null
        ||"".equals(sysPointUnitMapper.findByTwoId(sysDevicePointUnit.getUserDeviceId(),sysDevicePointUnit.getDataChannelId()))){
            sysPointUnitMapper.insertPointUnit(sysDevicePointUnit);
            SysUserDevice sysUserDevice = sysUserDeviceMapper.findPointNum(sysDevicePointUnit.getUserDeviceId());
            sysUserDevice.setPointNums(sysDevicePointUnit.getPointNums());
            sysUserDeviceMapper.updateUserDevice(sysUserDevice);
        }else{
            sysPointUnitMapper.updatePointUnit(sysDevicePointUnit);
            SysUserDevice sysUserDevice = sysUserDeviceMapper.findPointNum(sysDevicePointUnit.getUserDeviceId());
            sysUserDevice.setPointNums(sysDevicePointUnit.getPointNums());
            sysUserDeviceMapper.updateUserDevice(sysUserDevice);
        }
        return 1;
    }

    @Override
    public int insertPointUnit(SysDevicePointUnit sysDevicePointUnit) {

            SysUserDevice sysUserDevice = sysUserDeviceMapper.findPointNum(sysDevicePointUnit.getUserDeviceId());
                sysUserDevice.setPointNums(sysDevicePointUnit.getDataChannelId());
            sysUserDeviceMapper.updateUserDevice(sysUserDevice);
            sysPointUnitMapper.insertPointUnit(sysDevicePointUnit);
        return 1;
    }

    @Override
    public int deletePointUnit(Long userDeviceId, Long dataChannelId) {
        Long pointNums = dataChannelId-1L;
        SysUserDevice sysUserDevice = sysUserDeviceMapper.findPointNum(userDeviceId);
        sysUserDevice.setPointNums(pointNums);
        sysUserDeviceMapper.updateUserDevice(sysUserDevice);
        //根据两id判断获取到的数据是否为空
        if(sysPointUnitMapper.findByTwoId(userDeviceId,dataChannelId)!=null){
            sysPointUnitMapper.deletePointUnit(userDeviceId,dataChannelId);
        }
        return 1;
    }

    //获取数据的类型
    private static String getType(Object a) {
        return a.getClass().toString();

    }

}
